﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WPP_Service.Data;
using System.Data.SqlClient;

namespace WPP_Service.Test
{
    [TestClass]
    public class SubroutineTest
    {
        [TestMethod]
        public void TestGetOrderItems()
        {
            OrderItem oi;

            //for the first 10  order items....
            for (int orderitemid = 1; orderitemid < 10; orderitemid++)
            {
                oi = new OrderItem();
                //connect to database,
                using (SqlConnection sqlServerConnection = new SqlConnection("Data Source=23.23.80.142;Initial Catalog=WPP;User Id=sa;Password=Hermes_01;Pooling=False"))
                {
                    sqlServerConnection.Open();
                    SqlCommand sqlServerCommand;
                    //select all information for each, 
                    using (sqlServerCommand = new SqlCommand("SELECT OrderItem_id, item_customer_notes, item_description, item_possku, item_quantity, item_number FROM OrderItem WHERE OrderItem_id = " + orderitemid + ";", sqlServerConnection))
                    {
                        using (SqlDataReader reader = sqlServerCommand.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                //and set the data for the order item object    
                                oi.OrderItemID = (int)reader["OrderItem_id"];
                                oi.ItemCustomerNotes = reader["item_customer_notes"].ToString();
                                oi.ItemDescription = reader["item_description"].ToString();
                                oi.ItemNumber = reader["item_number"].ToString();
                                oi.ItemPossku = reader["item_possku"].ToString();
                                oi.ItemQuantity = (int)reader["item_quantity"];
                            }
                        }
                    }
                }
                //test to ensure none of the object data is null and that the id's are matching
                Assert.IsNotNull(oi);
                Assert.AreEqual(orderitemid, oi.OrderItemID);
                Assert.IsNotNull(oi.ItemDescription);
                Assert.IsNotNull(oi.ItemPossku);
                Assert.IsNotNull(oi.ItemQuantity);
                Assert.IsNotNull(oi.ItemNumber);
            }
        }

        [TestMethod]
        public void TestGetPackages()
        {
            Package p = new Package();
            //for each ofthe first 5 packages...
            for (int packageid = 1; packageid < 5; packageid++)
            {
                p = new Package();
                //connect to database,
                using (SqlConnection sqlServerConnection = new SqlConnection("Data Source=23.23.80.142;Initial Catalog=WPP;User Id=sa;Password=Hermes_01;Pooling=False"))
                {
                    sqlServerConnection.Open();
                    SqlCommand sqlServerCommand;
                    //select the number of packages,
                    using (sqlServerCommand = new SqlCommand("SELECT COUNT(*) FROM Package p JOIN OrderItem oi ON p.Package_id = oi.Package_id WHERE p.Package_id = " + packageid + ";", sqlServerConnection))
                    {
                        using (SqlDataReader reader = sqlServerCommand.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                p.OrderItems = new OrderItem[(int)reader[0]];
                            }
                        }
                    }
                    //select all the package information
                    using (sqlServerCommand = new SqlCommand("SELECT p.Package_id, cancelled, complete, multi_box, package_date_boxed, package_pickup_date, oi.OrderItem_id FROM Package p JOIN OrderItem oi ON p.Package_id = oi.Package_id WHERE p.Package_id = " + packageid + ";", sqlServerConnection))
                    {
                        using (SqlDataReader reader = sqlServerCommand.ExecuteReader())
                        {
                          
                            while (reader.Read())
                            {                               
                                //set the data in the package object
                                p.PackageID = (int)reader["Package_id"];
                                p.Cancelled = Convert.ToBoolean(reader["cancelled"]);
                                p.Complete = Convert.ToBoolean(reader["complete"]);
                                p.MultiBox = Convert.ToBoolean(reader["multi_box"]);
                                p.PackageDateBoxed = Convert.ToDateTime(reader["package_date_boxed"]);
                                p.PackagePickupDate = Convert.ToDateTime(reader["package_pickup_date"]);               
                            }
                        }
                    }
                }
                //test to ensure the package has no null data and that the id's are equal
                Assert.IsNotNull(p);
                Assert.AreEqual(packageid, p.PackageID);
                Assert.IsNotNull(p.Cancelled);
                Assert.IsNotNull(p.Complete);
                Assert.IsNotNull(p.MultiBox);
                Assert.IsNotNull(p.PackageDateBoxed);
                Assert.IsNotNull(p.PackagePickupDate);
                Assert.IsNotNull(p.OrderItems);
            }
        }

        [TestMethod]
        public void TestGetOrders()
        {
            Order o = new Order();
            //for the first 5 orders...
            for (int orderid = 1; orderid < 5; orderid++)
            {
                o = new Order();
                //connect to the database,
                using (SqlConnection sqlServerConnection = new SqlConnection("Data Source=23.23.80.142;Initial Catalog=WPP;User Id=sa;Password=Hermes_01;Pooling=False"))
                {
                    sqlServerConnection.Open();
                    SqlCommand sqlServerCommand;
                    //select the number of orders,
                    using (sqlServerCommand = new SqlCommand("SELECT COUNT(*) FROM Orders o JOIN Package p ON o.Order_id = p.Order_id WHERE o.Order_id = " + orderid + ";", sqlServerConnection))
                    {
                        using (SqlDataReader reader = sqlServerCommand.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                o.Packages = new Package[(int)reader[0]];
                            }
                        }
                    }
                    //select all the information from the order
                    using (sqlServerCommand = new SqlCommand("SELECT o.Order_id, order_billing_name, order_complete_date, order_creation_date, order_shipping_name, order_transaction_date, o.complete, order_process_date, p.Package_id FROM Orders o JOIN Package p ON o.Order_id = p.Order_id WHERE o.Order_id = " + orderid + ";", sqlServerConnection))
                    {
                        using (SqlDataReader reader = sqlServerCommand.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                    //set the data in the order object
                                    o.OrderID = (int)reader["Order_id"];
                                    o.OrderBillingName = reader["order_billing_name"].ToString();
                                    if (reader["order_complete_date"] != DBNull.Value) o.OrderCompleteDate = Convert.ToDateTime(reader["order_complete_date"]);
                                    o.OrderCreationDate = Convert.ToDateTime(reader["order_creation_date"]);
                                    o.OrderShippingName = reader["order_shipping_name"].ToString();
                                    o.OrderTransactionDate = Convert.ToDateTime(reader["order_transaction_date"]);
                                    o.Complete = Convert.ToBoolean(reader["complete"]);
                                    o.OrderProcessDate = Convert.ToDateTime(reader["order_process_date"]);
                             }
                        }
                    }
                }
                //test the order to ensure no data is null and the id's are equal
                Assert.IsNotNull(o);
                Assert.AreEqual(orderid, o.OrderID);
                Assert.IsNotNull(o.OrderBillingName);
                Assert.IsNotNull(o.OrderCreationDate);
                Assert.IsNotNull(o.OrderShippingName);
                Assert.IsNotNull(o.OrderTransactionDate);
                Assert.IsNotNull(o.Complete);
                Assert.IsNotNull(o.Cancelled);
                Assert.IsNotNull(o.OrderProcessDate);
                Assert.IsNotNull(o.Packages);
            }
        }
    }
}
